import { deconstructEvent } from "../../hooks/deconstructEvent";
import http from "../../utils/http";

// pages/rate/rate.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    current: 'RMB',
    show: false,
    exchang_data: [],
    baseCurrency: {
      exchang_id: 13, exchang_name: "人民币"
    },
    targetCurrency: {
      exchang_id: 1, exchang_name: "美元"
    },
    amountOfMoney: 0,
    currencyMappingTable: {},
    currencyCategory: '',
    finalAmount: 0
  },
  open(e) {
    this.setData({
      show: true
    })
    console.log(e, "YYYYYYYYYYYYYYYYYYYYYYYYY");
    const { currencycategory } = deconstructEvent(e)
    console.log(currencycategory);
    this.setData({
      currencyCategory: currencycategory
    })
  },
  onClose() {
    // console.log(this.data.finalAmount !== 0);
    
    if(this.data.finalAmount !== 0){
      this.setData({
        finalAmount:0
      })
    }
    this.setData({
      show: false
    })
  },
  change(e) {
    console.log(e)
    const { detail } = e
    const { value } = detail
    console.log(value, "GGGGGGGGGGGGGGGGGG");
    const monetaryStructure = this.data.currencyMappingTable.get(value)
    console.log(this.data.currencyCategory, "TGTGTGTGTGTG");

    if (this.data.currencyCategory === "基准货币") {
      this.setData({
        baseCurrency: monetaryStructure
      })
    } else if (this.data.currencyCategory === "目标货币") {
      this.setData({
        targetCurrency: monetaryStructure
      })
    }
    this.onClose();
  },
  exchangeRateCalculation() {
    console.log(this.data.baseCurrency);
    console.log(this.data.targetCurrency);
    console.log(this.data.amountOfMoney);
    const { exchang_id: nid } = this.data.baseCurrency
    const { exchang_id: mid } = this.data.targetCurrency
    const { amountOfMoney: number } = this.data
    http.post("/get/exchang/", {
      nid,
      mid,
      number
    }).then((res) => {
      console.log(res, "]]]]]]]]]]]]]]]]]]]]]");
      const { amount: finalAmount } = res.data
      this.setData({
        finalAmount
      })
    })
  },
  messageChang(e) {
    let amountOfMoney = e.detail.value
    this.setData({
      amountOfMoney
    })
  },
  // /get/exchang/list/
  getExchangList() {
    http.get("/get/exchang/list/").then((res) => {
      console.log(res);
      const { exchang_data } = res;
      this.setData({
        exchang_data
      })
      console.log(this.data.exchang_data);

      // 创建一个map，汇率计算时用
      const currencyMappingTable = new Map();
      const { data } = this
      const { exchang_data: exchang_data_clone } = data
      for (const i in exchang_data_clone) {
        currencyMappingTable.set((exchang_data_clone[i].exchang_name), (exchang_data_clone[i]))
      }
      console.log(currencyMappingTable);
      this.setData({
        currencyMappingTable
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.getExchangList()


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})